package {
	import com.youdo.ad.core.XAdSlotType;
	import com.youdo.ad.core.XAdSlot;
	import com.youdo.ad.core.XAdManager;
	import com.youdo.ad.loader.XAdManagerLoader;

	import flash.events.Event;

	import fl.video.FLVPlayback;

	import flash.display.Sprite;
	
	import flash.external.ExternalInterface;

	public class TestPlayer extends Sprite {
		private var playback : FLVPlayback;
		//
		private var theXAdManagerLoader : XAdManagerLoader;
		private var theXAdManager : XAdManager;
		//
		static private const VIDEO_URL : String = "http://220.181.154.17/youku/697629E871845831BD263C38C1/03000207004F4792BD145503BAF2B1158BC554-774B-1B2B-C1FC-BBCF7B0276BC.flv";

		public function TestPlayer() {
			this.addEventListener(Event.ADDED_TO_STAGE, this.handleAdded2Stage);
		}

		private function handleAdded2Stage(event : Event) : void {
			this.removeEventListener(Event.ADDED_TO_STAGE, this.handleAdded2Stage);
			//
			this.graphics.clear();
			this.graphics.beginFill(0x99FF00, 0.5);
			this.graphics.drawRect(0, 0, 800, 600);
			//
			this.theXAdManagerLoader = new XAdManagerLoader();
			this.theXAdManagerLoader.signal4Completed.add(this.handleXAdManagerLoadCompleted);
			this.theXAdManagerLoader.signal4IOError.add(this.handleXAdManagerLoadError);
			this.theXAdManagerLoader.signal4SecurityError.add(this.handleXAdManagerLoadError);
			this.theXAdManagerLoader.loadAdManager('http://127.0.0.1/a/AdManager.swf?', null, Math.random());
			//
			this.playback = new FLVPlayback();
			this.addChild(this.playback);
			this.playback.width = 400;
			this.playback.height = 300;
			this.playback.x = 70;
			this.playback.fullScreenTakeOver = false;
		}

		private function handleXAdManagerLoadCompleted() : void {
			trace("handleXAdManagerLoadCompleted");
			//
			this.playMainVideo();

			this.theXAdManager = this.theXAdManagerLoader.newAdManager();
			ExternalInterface.call("console.log", "TestPlayer.handleXAdManagerLoadCompleted() theXAdManager="+this.theXAdManager);

			//
			//this.theXAdManager.signal4RequestCompleted.addOnce(this.handleXAdManagerRequestCompleted);
			//this.theXAdManager.signal4SlotStart.add(this.handleSlotStart);
			//this.theXAdManager.signal4SlotEnd.add(this.handleSlotEnd);
			//
			//this.theXAdManager.registerPlayheadTimeCallback(this.getPlayheadTime);
			//this.theXAdManager.registerVideoDisplay(this);
			//
			//this.theXAdManager.request(1000);
		}

		private function getPlayheadTime() : Number {
			return this.playback ? this.playback.playheadTime:0;
		}

		private function handleXAdManagerLoadError() : void {
			trace("handleXAdManagerLoadError");
			ExternalInterface.call("console.log", "TestPlayer.handleXAdManagerLoadError()");
			this.playMainVideo();
		}

		public function playMainVideo() : void {
			this.playback.source = VIDEO_URL;
		}

		private function handleXAdManagerRequestCompleted(success : Boolean) : void {
			ExternalInterface.call("console.log", "TestPlayer.handleXAdManagerRequestCompleted("+success+")");
			if (success) {
				var slot : XAdSlot;
				var slots : Vector.<XAdSlot> = this.theXAdManager.getTemporalSlots();
				for (var i : int = 0; i < slots.length; i++) {
					if (slots[i].getType() == XAdSlotType.PREOLL) {
						slot = slots[i];
						break;
					}
				}
				if (slot) {
					slot.play();
				} else {
					this.playMainVideo();
				}
			} else {
				this.handleXAdManagerLoadError();
			}
		}
		private function handleSlotStart():void {
			
		}
		private function handleSlotEnd():void {
			
		}
		
		private function handleSlotEnded():void {
			this.playMainVideo();
		}
	}
}